package com.cbgolf.oa.activity.bill;

import android.os.Bundle;
import android.support.annotation.Nullable;
import com.cbgolf.oa.R;
import com.cbgolf.oa.base.BaseActivity;
import com.cbgolf.oa.entity.PointBean;
import com.cbgolf.oa.entity.ShopBean;
import com.cbgolf.oa.model.ShoppingModel;
import com.cbgolf.oa.presenter.IShopPresenter;
import com.cbgolf.oa.util.AnimaUtil;
import com.cbgolf.oa.util.Util;
import com.cbgolf.oa.views.ShoppingView;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lq on 2017/10/31.
 */

public class ShoppingActivity extends BaseActivity implements IShopPresenter {

    private ShoppingView view;
    private ShoppingModel model;

    List<PointBean> listPoint;
    private boolean getPointFail;

    private String pointCode;
    private String cardId;
    private String userId;
    private String cardNum;
    private String orderTime;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.a_shopping);
    }

    @Override
    protected void findViews() {
        view = new ShoppingView(this, this);
        model = new ShoppingModel(this);
    }

    @Override
    public String getActivityTitle() {
        return "";
    }

    @Override
    public void back() {
        finish();
    }

    @Override
    public void showProgress() {
        AnimaUtil.showLoading(context);
    }

    @Override
    public void stopProgress() {
        AnimaUtil.stopLoading();
    }

    @Override
    public void getDataFail(String msg) {

    }

    @Override
    public void getDataSuccess(Object obj) {
        view.getDataSuccess(obj);
    }

    @Override
    public void getPointFail() {
        getPointFail = true;
    }

    @Override
    public void getPointSuccess(List<PointBean> list) {
        getPointFail = false;
        if (Util.listIsNull(list)) {
            view.showNoPoint("没有营业点");
            return;
        }
        listPoint = list;
        view.showPoint(list.get(0).name);
        pointCode = list.get(0).code;
        model.getData(list.get(0).code);
    }

    @Override
    public void getDataFail(String msg, int code) {
        view.getDataFail(msg, code);
    }

//    @Override
//    public void getDataSuccess(List<ShopBean> list) {
//        view.showShopData(list);
//    }


    @Override
    public void selectCardNumFail() {
        view.setSelectCardNumFail();
        Util.showNetError();
    }

    @Override
    public void selectCardNumSuccess(List<ShopBean> list) {
        if (!Util.listIsNull(list)) {
            cardId = list.get(0).consumerCarId;
            userId = list.get(0).customerId;
            cardNum = list.get(0).consumerCarNo;
        }
        view.showCardNumInfo(list);
    }

    @Override
    public void submitFail(String msg) {
        Util.show(msg);
        view.setSubmitEnable(true);
    }

    @Override
    public void submitSuccess(String id) {
        mStartActivity(BillingSuccessActivity.class, "id", id);
        finish();
    }

    @Override
    public void ClickTopRight() {
        Map<String, String> map = new HashMap<>();
        map.put("pointCode", pointCode);
//        map.put("cardNum", cardNum);
//        map.put("orderTime","");
//        map.put("userId",userId);
        mStartActivity(BillHistory.class, map);
    }

    @Override
    public void ClickChangePoint() {
        if (getPointFail) {
            Util.show("营业点获取异常，请稍后再试");
            return;
        }
        if (Util.listIsNull(listPoint)) {
            Util.show("暂无营业点信息");
            return;
        }
        view.ChangePoint(listPoint);
    }

    @Override
    public void ClickNumOk(String num) {
        model.selectCardNum(num);
    }

    @Override
    public void ClickShopCar() {

    }

    @Override
    public void ClickSubMit(List<ShopBean.goodsDetails> list, String point) {
        ShopBean bean = new ShopBean();
        bean.pointCode = Util.isNull(point) ? pointCode : point;
        bean.userId = userId;
        bean.cardId = cardId;
        bean.goods = list;
        model.subMit(bean);

    }

    @Override
    public void getGoods(String code) {
        model.getData(code);
    }
}
